/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TransferForm.java
 *
 * Created on Sep 13, 2012, 10:26:36 AM
 */
package atm;

import controller.AccountDetailRemote;
import controller.AccountTransferRemote;
import controller.CheckAccountRemote;
import entities.Account;
import java.awt.Color;
import java.math.BigDecimal;
import javax.ejb.EJB;

/**
 *
 * @author ThanhNS
 */
public class TransferForm extends javax.swing.JFrame {

    @EJB
    private static AccountTransferRemote accountTransfer;
    @EJB
    private static CheckAccountRemote checkAccount;
    @EJB
    private static AccountDetailRemote accountDetail;
    private Account accReceiver = null;
    private Account accSender = null;

    /** Creates new form TransferForm */
    public TransferForm() {
        initComponents();
    }

    public TransferForm(Account account) {
        initComponents();
        this.accSender = account;
        setResizable(Boolean.FALSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        pnlTransferTo = new javax.swing.JPanel();
        lblReceiverCode = new javax.swing.JLabel();
        txtReceiverCode = new javax.swing.JTextField();
        btnVerifyAccount = new javax.swing.JButton();
        pnlReceiverDetails = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblVerifyResult = new javax.swing.JLabel();
        pnlAmountTransfer = new javax.swing.JPanel();
        txtAmountTransfer = new javax.swing.JTextField();
        btnTransfer = new javax.swing.JButton();
        lblAmountTransfer = new javax.swing.JLabel();
        lblTranserResult = new javax.swing.JLabel();
        btnBack = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("TRANSFER ");

        pnlTransferTo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Transfer To", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century", 1, 11))); // NOI18N

        lblReceiverCode.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblReceiverCode.setText("Enter Receiver Account Code:");

        btnVerifyAccount.setText("Verify Account");
        btnVerifyAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerifyAccountActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlTransferToLayout = new javax.swing.GroupLayout(pnlTransferTo);
        pnlTransferTo.setLayout(pnlTransferToLayout);
        pnlTransferToLayout.setHorizontalGroup(
            pnlTransferToLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTransferToLayout.createSequentialGroup()
                .addGroup(pnlTransferToLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlTransferToLayout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(lblReceiverCode))
                    .addGroup(pnlTransferToLayout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addComponent(txtReceiverCode, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlTransferToLayout.createSequentialGroup()
                        .addGap(129, 129, 129)
                        .addComponent(btnVerifyAccount)))
                .addContainerGap(95, Short.MAX_VALUE))
        );
        pnlTransferToLayout.setVerticalGroup(
            pnlTransferToLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTransferToLayout.createSequentialGroup()
                .addComponent(lblReceiverCode)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtReceiverCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnVerifyAccount))
        );

        pnlReceiverDetails.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Receiver Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Courier New", 1, 11))); // NOI18N

        lblName.setText("Receiver Name:");

        lblAddress.setText("Receiver Address:");

        javax.swing.GroupLayout pnlReceiverDetailsLayout = new javax.swing.GroupLayout(pnlReceiverDetails);
        pnlReceiverDetails.setLayout(pnlReceiverDetailsLayout);
        pnlReceiverDetailsLayout.setHorizontalGroup(
            pnlReceiverDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlReceiverDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlReceiverDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAddress)
                    .addComponent(lblVerifyResult, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                    .addComponent(lblName))
                .addContainerGap())
        );
        pnlReceiverDetailsLayout.setVerticalGroup(
            pnlReceiverDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlReceiverDetailsLayout.createSequentialGroup()
                .addComponent(lblVerifyResult, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblName, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pnlAmountTransfer.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Transfer Process", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century", 1, 11))); // NOI18N

        btnTransfer.setText("Transfer");
        btnTransfer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTransferActionPerformed(evt);
            }
        });

        lblAmountTransfer.setFont(new java.awt.Font("Century", 1, 11));
        lblAmountTransfer.setText("Enter Amount You Want To Transfer:");

        javax.swing.GroupLayout pnlAmountTransferLayout = new javax.swing.GroupLayout(pnlAmountTransfer);
        pnlAmountTransfer.setLayout(pnlAmountTransferLayout);
        pnlAmountTransferLayout.setHorizontalGroup(
            pnlAmountTransferLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAmountTransferLayout.createSequentialGroup()
                .addGroup(pnlAmountTransferLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlAmountTransferLayout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(btnTransfer))
                    .addGroup(pnlAmountTransferLayout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(lblAmountTransfer))
                    .addGroup(pnlAmountTransferLayout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(txtAmountTransfer, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(71, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlAmountTransferLayout.createSequentialGroup()
                .addContainerGap(108, Short.MAX_VALUE)
                .addComponent(lblTranserResult, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(106, 106, 106))
        );
        pnlAmountTransferLayout.setVerticalGroup(
            pnlAmountTransferLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAmountTransferLayout.createSequentialGroup()
                .addComponent(lblAmountTransfer)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtAmountTransfer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnTransfer)
                .addGap(11, 11, 11)
                .addComponent(lblTranserResult, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnBack.setText("Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pnlReceiverDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pnlAmountTransfer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pnlTransferTo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnBack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 268, Short.MAX_VALUE)
                        .addComponent(btnReset)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlTransferTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlReceiverDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(pnlAmountTransfer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBack)
                    .addComponent(btnReset))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVerifyAccountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerifyAccountActionPerformed
        // TODO add your handling code here:        
        if (!txtReceiverCode.getText().matches("\\d{13}")) {
            lblVerifyResult.setText("The account code must be include 13 digits!");
            lblVerifyResult.setForeground(Color.red);
            lblName.setText("");
            lblAddress.setText("");
            pnlTransferTo.setEnabled(false);
        } else if (accSender.getAcountCode().equals(txtReceiverCode.getText())) {
            lblVerifyResult.setText("Your account cannot transfer with itself!");
            lblVerifyResult.setForeground(Color.red);
            lblName.setText("");
            lblAddress.setText("");
            pnlTransferTo.setEnabled(false);
        } else if (!checkAccount.checkAccount(txtReceiverCode.getText())) {
            lblVerifyResult.setText("Account you want transfer to is not exist!");
            lblVerifyResult.setForeground(Color.red);
            lblName.setText("");
            lblAddress.setText("");
            pnlTransferTo.setEnabled(false);
        } else {
            lblVerifyResult.setText("Please check information below about receiver:");
            lblVerifyResult.setForeground(Color.GREEN);
            accReceiver = accountDetail.accountDetail(txtReceiverCode.getText());

            lblName.setForeground(Color.blue);
            lblAddress.setForeground(Color.blue);
            lblName.setText("Receiver Name: " + accReceiver.getAccountName());
            lblAddress.setText("Receiver Address: " + accReceiver.getAddress());
            pnlTransferTo.setEnabled(true);
        }
    }//GEN-LAST:event_btnVerifyAccountActionPerformed

    private void btnTransferActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTransferActionPerformed
        // TODO add your handling code here:
        String sAmount = txtAmountTransfer.getText();
        if (sAmount == null || sAmount.equals("")) {
            lblTranserResult.setText("Enter amount for transfer!");
        } else {
            try {
                double amount = Double.parseDouble(sAmount);
                BigDecimal balance = accSender.getBalance();
                if ((balance.doubleValue() - 100000) < 0) {
                    lblTranserResult.setText("Your balance is not enought for transaction!");
                    lblTranserResult.setForeground(Color.red);
                } else if (amount % 50000 != 0) {
                    lblTranserResult.setText("Amount for transfer must be multiples of 50000!");
                    lblTranserResult.setForeground(Color.red);
                } else if ((balance.doubleValue() - 50000) < amount) {
                    lblTranserResult.setText("Your balance is not enought to perform this transaction!");
                    lblTranserResult.setForeground(Color.red);
                } else {
                    if (accountTransfer.transfer(accSender, accReceiver, BigDecimal.valueOf(amount))) {
                        lblTranserResult.setText("Transfer successful!");
                        lblTranserResult.setForeground(Color.GREEN);
                    } else {
                        lblTranserResult.setText("Transfer failure!");
                        lblTranserResult.setForeground(Color.BLUE);
                    }
                }
            } catch (Exception e) {
                lblTranserResult.setText("Amount for transfer must be numberic!");
                lblTranserResult.setForeground(Color.red);
            }
        }
    }//GEN-LAST:event_btnTransferActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        // TODO add your handling code here:
        this.dispose();
        Show show = new Show(accSender);
        show.setLocationRelativeTo(null);
        show.setVisible(true);
    }//GEN-LAST:event_btnBackActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        txtReceiverCode.setText("");
        lblVerifyResult.setText("");
        lblName.setText("");
        lblAddress.setText("");
        pnlTransferTo.setEnabled(false);
    }//GEN-LAST:event_btnResetActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new TransferForm().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnTransfer;
    private javax.swing.JButton btnVerifyAccount;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblAmountTransfer;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblReceiverCode;
    private javax.swing.JLabel lblTranserResult;
    private javax.swing.JLabel lblVerifyResult;
    private javax.swing.JPanel pnlAmountTransfer;
    private javax.swing.JPanel pnlReceiverDetails;
    private javax.swing.JPanel pnlTransferTo;
    private javax.swing.JTextField txtAmountTransfer;
    private javax.swing.JTextField txtReceiverCode;
    // End of variables declaration//GEN-END:variables
}
